package com.ruoyi.ly.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ly.domain.ServeExamineFlow;
import com.ruoyi.ly.domain.ServeExamineFlowProject;
import com.ruoyi.ly.domain.dto.FlowAppDTO;
import com.ruoyi.ly.domain.dto.PageServeExamineFlowDTO;
import com.ruoyi.ly.domain.dto.ServeFlowPlanAppDTO;
import com.ruoyi.ly.domain.vo.ConfigIdandNameVO;
import com.ruoyi.ly.domain.vo.PageServeExamineFlowVO;
import com.ruoyi.ly.domain.vo.ServeExamineFlowDetailVO;
import com.ruoyi.ly.mapper.ServeExamineFlowMapper;
import com.ruoyi.ly.mapper.ServeExamineFlowProjectMapper;
import com.ruoyi.ly.service.ServeExamineFlowService;
import com.ruoyi.ly.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class ServeExamineFlowServiceImpl implements ServeExamineFlowService {

    @Autowired
    private ServeExamineFlowMapper serveExamineFlowMapper;
    @Autowired
    private ServeExamineFlowProjectMapper serveExamineFlowProjectMapper;

    //分页查询巡检记录
    @Override
    public PageResult pageQuery(PageServeExamineFlowDTO pageServeExamineFlowDTO) {
        PageHelper.startPage( pageServeExamineFlowDTO.getPage(), pageServeExamineFlowDTO.getPageSize());
        Page<PageServeExamineFlowVO> page = serveExamineFlowMapper.pageQuery(pageServeExamineFlowDTO);
        long total = page.getTotal();
        List<PageServeExamineFlowVO> records = page.getResult();
        return new PageResult(total,records);
    }


    //查询巡检记录详情
    @Override
    public ServeExamineFlowDetailVO selectById(Long examineFlowId) {
        ServeExamineFlowDetailVO serveExamineFlowDetailVO = serveExamineFlowMapper.selectById(examineFlowId);
        //TODO 巡检项

        List<ConfigIdandNameVO> examineItems =  serveExamineFlowMapper.slectAll(examineFlowId);
        serveExamineFlowDetailVO.setExamineItems(examineItems);
        return serveExamineFlowDetailVO;
    }


    //提交巡检记录(小程序)
    @Override
    public void add(ServeFlowPlanAppDTO serveFlowPlanAppDTO) {

        ServeExamineFlow serveExamineFlow = new ServeExamineFlow();
        BeanUtils.copyProperties(serveFlowPlanAppDTO,serveExamineFlow);



        List<FlowAppDTO> flowAppDTOS = serveFlowPlanAppDTO.getFlowAppDTOS();

        AtomicInteger errorItems = new AtomicInteger();//异常项的数量

        flowAppDTOS.forEach(flowAppDTO ->{
            ServeExamineFlowProject serveExamineFlowProject = new ServeExamineFlowProject();
            serveExamineFlowProject.setFlowProjectWords(flowAppDTO.getFlowProjectWords());

            serveExamineFlowProject.setSelectValue(flowAppDTO.getSelectValue());
            //对巡检项是否异常进行判断
            if(flowAppDTO.getSelectValue().equals("1")){
                int andIncrement = errorItems.getAndIncrement();
                andIncrement++;
                errorItems.set(andIncrement);
            }

            serveExamineFlowProject.setExamineConfigId(flowAppDTO.getExamineConfigIds());

            //处理前端传来的数组
            String flowProjectImgs = StringUtils.strip(flowAppDTO.getFlowProjectImgs().toString(), "[]");

            System.out.println(flowProjectImgs);

            serveExamineFlowProject.setFlowProjectImgs(flowProjectImgs);

            serveExamineFlowProject.setCreateTime(new Date());
            serveExamineFlowProject.setExamineFlowId(serveFlowPlanAppDTO.getExamineFlowId());
            //TODO 创建人id用openID对应
            //serveExamineFlowProject.setCreateUid(serveExamineFlowProject.getCreateUid());
            //插入巡检记录选项表
            serveExamineFlowProjectMapper.insert(serveExamineFlowProject);
        });


        //增加巡检记录表里面的陪同巡检人  流水附件 计划描述 并且将keyId的值赋为1
        serveExamineFlow.setKeyId(1l);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        serveExamineFlow.setCreateTime(formatter.format(date));
        //巡检项是否异常  刚开始默认插入为0是正常 现在进行判断看是否有异常出现 如果有异常出现则将值改为1
        if(errorItems.getAndIncrement()>0)
        {
            //出现异常
            serveExamineFlow.setIsUnusual(errorItems.getAndIncrement());//将异常项目数量赋值给isUnsual 将这个字段怆传给前端
        }

        serveExamineFlowMapper.add(serveExamineFlow);
    }

    //查询巡检记录详情(未巡检时小程序端使用)
    @Override
    public ServeExamineFlowDetailVO selectByIdApp(Long examineFlowId) {
        ServeExamineFlowDetailVO serveExamineFlowDetailVO = serveExamineFlowMapper.selectById(examineFlowId);
        //TODO 巡检项

        List<ConfigIdandNameVO> examineItems =  serveExamineFlowMapper.slectAllApp(examineFlowId);
        serveExamineFlowDetailVO.setExamineItems(examineItems);
        return serveExamineFlowDetailVO;
    }
}
